package logic

import (
	"context"
	"fmt"

	"longbri/api/main/internal/svc"
	"longbri/api/main/internal/types"
	"longbri/rpc/pb/dashboard"

	"github.com/zeromicro/go-zero/core/logx"
)

type RankListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRankListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RankListLogic {
	return &RankListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RankListLogic) RankList(req *types.ReqRankList) (*types.RspRankList, error) {
	// todo: add your logic here and delete this line
	fmt.Printf("dada %d\n", req.PageNum)

	rsp, err := l.svcCtx.Dashboard.RankList(l.ctx, &dashboard.RankListReq{
		StartDate:      req.StartDate,
		EndDate:        req.EndDate,
		CurPageNum:     int32(req.PageNum),
		PerPageCount:   int32(req.PerPageCount),
		UpDivChange:    int32(req.UpDivChange),
		MarketValStart: int32(req.MarketValStart),
		MarKetValEnd:   int32(req.MarKetValEnd),
		UpRation:       int32(req.UpRatio),
		ChangeRatio:    int32(req.ChangeRatio),
	})

	fmt.Printf("rsp TotalCount %d\n", rsp.TotalCount)
	if err != nil {
		logx.Error(err)
		return &types.RspRankList{}, err
	}

	var respone []types.RankInfo

	for i := 0; i < len(rsp.RankInfos); i++ {
		tmp := rsp.RankInfos[i]
		respone = append(respone, types.RankInfo{
			Date:                   tmp.Date,
			Code:                   tmp.Code,
			Name:                   tmp.Name,
			MarketVal:              tmp.MarketVal,
			ChangeRatio:            tmp.ChangeRatio,
			TradeChangeRatio:       tmp.TradeChangeRatio,
			CirculationMarketValue: tmp.CirculationMarketValue,
			TotalShares:            tmp.TotalShares,
			CirculationShares:      tmp.CirculationShares,
			PriceNominal:           tmp.PriceNominal,
			TradeTrunover:          tmp.TradeTrunover,
			TradeVolumn:            tmp.TradeVolumn,
			PriceAmplitude:         tmp.PriceAmplitude})
	}

	return &types.RspRankList{
		RankList:     respone,
		TotalCount:   int(rsp.TotalCount),
		PerPageCount: int(rsp.PerPageCount),
		CurPageNum:   int(rsp.CurPageNum),
	}, nil
}
